<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>链接</button>
    <button>发送数据</button><br>
    <!-- <button>发送数据</button><br> -->
    接收的数据
    <span></span>
    <script>
        var btn = document.querySelectorAll('button');
        var span = document.querySelector('span');
        const ws = new WebSocket('ws://localhost:9998')

        btn[0].onclick = function () {
            ws.onopen = () => {
                console.log('链接成功');
                // console.log(btn[1].style.disabled);
                btn[1].disabled = false;
            }
            ws.onclose = () => {
                console.log('链接失败');
                btn[1].disabled = true;

            }
            ws.onmessage = msg => {
                console.log('接收的数据');
                console.log(msg.data);
                span.innerHTML = msg.data
            }
        }
        btn[1].onclick = function () {
            ws.send(JSON.stringify({
                action: 'getData',
                socketType: 'trendData',
                chartName: 'trend',
                value: ''
            }))
        }
    </script>
</body>

</html>